import { _decorator, easing, SpriteComponent, Label, Widget, sp, assetManager, Camera, Node, Color, math, Sprite, SystemEvent, LabelComponent, SkinnedMeshRenderer, Prefab, v3, Vec3, SkeletalAnimation, ProgressBar, tween, UITransform, sys, SpriteFrame, UITransformComponent } from 'cc';

import BaseView from '../../Script/game/BaseView';
import { Bullet } from '../../Script/config/BaseData';
import Tool from '../../Script/tool/Tool'
// import { luckyBox } from './luckyBox';
import GD from '../../Script/config/GD';
import GDef from '../../Script/config/GDef';
import SdkMgr from '../../Script/tyq3.x/SdkMgr';
import TyqEventMgr from '../../Script/tyq3.x/tyq-event-mgr';
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preFrame')
export class preFrame extends BaseView {
    @property({
        type: Node
    })
    btnGiveup: Node = null;
    @property({
        type: Node
    })
    btnPlay: Node = null;
    @property({
        type: Node
    })
    nodeAtt: Node = null;
    @property({
        type: Node
    })
    nodeLight: Node = null;
    @property({
        type: Label
    })
    labelTitle: Label = null;
    @property({
        type: Label
    })
    labelHp: Label = null;
    @property({
        type: Label
    })
    labelAttack: Label = null;
    @property({
        type: Label
    })
    labelCoins: Label = null;
    @property({
        type: Label
    })
    labelArm: Label = null;
    @property({
        type: SpriteFrame
    })
    sprBtn: SpriteFrame = null;

    private nodeArm: Node = null;
    private num: number = 0;
    onLoad() {
        this.bindButton(this.btnGiveup, () => {
            this.onClose();
        });
        this.bindButton(this.btnPlay, () => {
            TyqEventMgr.ins.sendEvent('观看激励视频-解锁弹窗获得多倍金币');
            SdkMgr.ins.showRewardAd("观看激励视频-解锁弹窗获得多倍金币", () => {
                TyqEventMgr.ins.sendEvent('观看完激励视频-解锁弹窗获得多倍金币');
                this.emit(GDef.wdEvent.playEffect, "CoinCollect");
                this.emit(GDef.wdEvent.addCoin, this.jsonArm.exp * 20, GDef.getRoot());
                this.onClose();
            }, () => {
            }, () => {
            })

        });
    }
    show(type: number, tid: number, num: number = 0) {
        console.log("show:", type, tid);
        this.emit(GDef.wdEvent.playEffect, "Upgrade");
        this.num = 0;
        this.nodeLight.destroyAllChildren();
        this.nodeAtt.active = false;
        this.num = num;
        console.log("show Num:" + this.num);
        this.jsonArm = jsonTables.getJsonTableObj(type == 1 ? "human" : "dino", tid);
        if (!this.jsonArm) return;
        this.jsonArmType = jsonTables.getJsonTableObj(this.jsonArm.armType == GDef.TypeHuman ? "humanType" : "dinoType", this.jsonArm.type);
        if (!this.jsonArmType) return;
        if (Tool.isInVerify()) {
            this.btnGiveup.getComponent(Sprite).spriteFrame = this.sprBtn;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.width = this.btnPlay.getComponent(UITransformComponent).contentSize.width;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.height = this.btnPlay.getComponent(UITransformComponent).contentSize.height;
            this.scheduleOnce(() => {
                this.btnGiveup.setPosition(v3(0, -180, 0));
            }, 0.01);
        }
        else {
            this.btnGiveup.getComponent(Sprite).spriteFrame = null;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.width = this.btnGiveup.children[0].getComponent(UITransformComponent).contentSize.width;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.height = this.btnGiveup.children[0].getComponent(UITransformComponent).contentSize.height;
            this.btnGiveup.setPosition(v3(0, -100, 0));
        }
        this.createDino();
        this.setAtt();
        tween(this.node.getChildByName("nodeBorder")).set({ scale: v3(0, 0, 0) }).to(0.3, { scale: v3(1, 1, 1) }, { easing: easing.elasticOut }).start();
    }
    createDino() {
        if (this.nodeArm) {
            GDef.poolArm.put(this.nodeArm);
            this.nodeArm = null;
        }
        var self = this;
        //生成一只恐龙
        Tool.initArm(1, GDef.nodeArms, GDef.SidePlayer, v3(0, 0.8, 3.8), v3(0, this.jsonArm.armType == GDef.TypeHuman ? -15 : 50, 0), this.jsonArmType, this.jsonArm, 0, GDef.LayerUI3D, 1.4).then((node) => {
            if (self) {
                self.nodeArm = node;
                self.nodeArm.scale = v3(0, 0, 0);
                tween(self.nodeArm).to(0.3, { scale: v3(1, 1, 1) }).start();
            }
            else if (node) {
                GDef.poolArm.put(node);
            }
        });
    }
    setAtt() {
        // this.nodeAtt.active = true;
        // this.labelHp.string = Tool.formatNum(parseInt(this.jsonArm.hp));
        // this.labelAttack.string = Tool.formatNum(parseInt(this.jsonArm.attack));
        this.labelCoins.string = Tool.formatNum(parseInt(this.jsonArm.exp * 20));
        this.labelTitle.string = "获得新的" + (this.jsonArmType.armType == GDef.TypeDino ? "恐龙" : "猎人");
        console.log("setAtt:", this.jsonArmType);
        this.labelArm.string = (this.jsonArm.quality == 2 ? GDef.strKey : "") + this.jsonArmType.title + (this.num ? (" x " + this.num) : "");
    }
    onClose() {
        //关闭前判断是否还有通知
        if (GDef.eventListShowFrame.length > 0) {
            this.show(GDef.eventListShowFrame[0]["type"], GDef.eventListShowFrame[0]["tid"], GDef.eventListShowFrame[0]["num"]);
            GDef.eventListShowFrame.splice(0, 1);
        }
        else this.node.destroy();
    }
    onDestroy() {
        if (this && this.nodeArm) {
            GDef.poolArm.put(this.nodeArm);
            this.nodeArm = null;
        }
    }
}
